package com.ht.right.action;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.ht.common.entity.PageInfo;
import com.ht.right.entity.BaseEntity;
import com.ht.right.service.CommonFacadeService;
import com.ht.right.util.CommonUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CommonFacadeAction extends ActionSupport implements ModelDriven<BaseEntity> {

	private static final long serialVersionUID = 1L;
	private CommonFacadeService<BaseEntity> commonFacadeService;
	private BaseEntity baseEntity;
	
	@Override
	public BaseEntity getModel() {
		if(CommonUtil.isNullOrEmpty(baseEntity)){
			baseEntity = new BaseEntity();
		}
		return baseEntity;
	}
	
	public String getEntityById(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uuid = request.getParameter("uuid");
		String className = request.getParameter("className");
		baseEntity = commonFacadeService.getEntityByID(className, uuid);
		return "success";
	}
	
	public String loadAllList() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String menuId = request.getParameter("menuId");
		CommonUtil.toJsonStr(ServletActionContext.getResponse(), commonFacadeService.loadAllList(menuId));
		return null;
	}

	public String loadListForPage() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String menuId = request.getParameter("menuId");
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String sortField = request.getParameter("sortField");
		String sortOrder = request.getParameter("sortOrder");
		PageInfo pageInfo = new PageInfo(pageIndex, pageSize, sortField, sortOrder);
		HttpSession session = request.getSession();
		String orgIds = (String) session.getAttribute("orgIds");
		CommonUtil.toJsonStr(ServletActionContext.getResponse(), commonFacadeService.loadListForPage(menuId, pageInfo,orgIds));
		return null;
	}
	
	public String deleteModelId() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String uuid = request.getParameter("uuid");
		String className = request.getParameter("className");
		commonFacadeService.deleteId(className, "uuid", uuid);
		return null;
	}

	public BaseEntity getBaseEntity() {
		return baseEntity;
	}
	
	public void setBaseEntity(BaseEntity baseEntity) {
		this.baseEntity = baseEntity;
	}
	
	public CommonFacadeService<BaseEntity> getCommonFacadeService() {
		return commonFacadeService;
	}

	public void setCommonFacadeService(CommonFacadeService<BaseEntity> commonFacadeService) {
		this.commonFacadeService = commonFacadeService;
	}

	
}
